const db = require('../sql/dbconfig')
exports.studentAccept = (req,res)=>{
    let sql = 'select * from student'
    // console.log(sql);
    db.query(sql , (err,data) =>{
         res.send({
            code:200,
            msg:'ok',
            data:data
        })
        // console.log(data);
    })
}

// 向数据库中添加数据
exports.addsmsgs = (req,res) =>{
    // console.log(req.query);
    let {name,age,sex,date,address} = req.query
    // console.log(name,age,sex,date,address);
    let sql = `insert into student(name,age,sex,date,address) value(?,?,?,?,?)`

    let data = [name,age,sex,date,address]

    // console.log(data);
    db.query(sql,data,(err,results,fields) => {
        if(err) console.log(err);
        res.send({
            code:200,
            msg:'添加成功'
        })
    })
}


// 删除数据
exports.delesmsg = (req,res) =>{
    // console.log(req.query);

    // 接收前端传过来的数据
    let multipleSelection = req.query.multipleSelection
    // console.log(multipleSelection);

    // 需要遍历返回值用map forEach返回的是个空值
    let arrid = multipleSelection.map(item => {
        // console.log(item.id);
        return item.id
    });

    // 数组拼接为 id in (?,?,?)这样的
    let value = arrid.join(',')
    // console.log(value);
    db.query('delete from student where id in ('+ value +')',(err,result)=>{
        res.send({
            code:200,
            mage:'删除成功'
        })
    })

}

// 搜索
exports.searchmsg= (req,res) =>{
    // console.log(req.query);
    let cont = req.query.content
    console.log(cont);
    let sql = `select * from student where name like '%${cont}%'`
    db.query(sql,(err, data) => {
        if (err) return;
        // else {
            res.send({
                data:data
            });
        // }
    })
}

